package com.xlro.servlet;


import com.xlro.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    private LoginService loginService = new LoginService();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);

//        resp.setContentType("text/html;charset=gb2312");
//        resp.setCharacterEncoding("utf-8");

//        getInitParameter("");//获得局部参数   session获取
//        getServletContext().getInitParameter("");//获得全局参数 application获取

        //传入的参数为学号
//        NIITDevelop.INSTANCE.init("2002922212");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        String role = req.getParameter("role");


        String title = "登录失败";
        String result = null;
        result = loginService.login(username,password,role,req.getSession());

        req.getSession().setAttribute("role",role);


        if("登陆成功".equals(result)){
            resp.sendRedirect("./main.jsp");
        }else {
//            resp.getWriter().println("<script>alert('"+result+"')</script>");
            req.getRequestDispatcher("./index.jsp?title="+title+"&message="+result).forward(req,resp);
//            resp.getWriter().println("<script src='layui/layui.js'></script> <script>layui.use('layer', function(){layui.layerl.msg('hello')</script>"  );
        }
    }

}
